package com.example.config.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.entity.MyTestEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: liu
 * 2022-03-30-14:10
 **/

public class MyTestListener extends AnalysisEventListener<MyTestEntity> {

    private static List<MyTestEntity> myTestEntities = new ArrayList<>();

    @Override
    public void invoke(MyTestEntity data, AnalysisContext analysisContext) {
        myTestEntities.add(data);
        System.out.println("解析到一条数据：" + data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("全部解析完成");
    }

    public static List<MyTestEntity> getMyTestEntities(){
        return myTestEntities;
    }

//    public void onApplicationEvent(ApplicationEvent event) {
//        if (event instanceof AuthenticationFailureBadCredentialsEvent) {
//            AuthenticationFailureBadCredentialsEvent authEvent = (AuthenticationFailureBadCredentialsEvent) event;
//            Authentication authentication = (Authentication) authEvent.getSource();
//            String username = (String) authentication.getPrincipal();
//            addCount(username);
//        }
//        if (event instanceof AuthenticationSuccessEvent) {
//            AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;
//            Authentication authentication = (Authentication) authEvent.getSource();
//            UserInfo userInfo = (UserInfo) authentication.getPrincipal();
//            String username = userInfo.getUsername();
//            cleanCount(username);
//        }
//    }

//    protected void addCount(String username) {
//        Map lockUserMap = getLockUserMap();
//        Integer count = lockUserMap.get(username);
//        if (count == null) {
//            lockUserMap.put(username, Integer.valueOf(1));
//        } else {
//            int resultCount = count.intValue() + 1;
//            if (resultCount >= 3) {
//                UserInfo userInfo = (UserInfo) userInfoService.loadUserByUsername(username);
//                userInfo.lockAccount();
//            } else {
//                lockUserMap.put(username, Integer.valueOf(resultCount));
//            }
//        }
//    }



}
